<?php
/**
 * TimesHelper.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/11/3- 2:21 下午
 */

namespace common\helpers;


class TimesHelper
{

    /**
     * 格式化时间戳
     * @param int $timestamp
     * @return false|string
     */
    public static function formatTimestamp(int $timestamp)
    {
        return date('Y-m-d H:i',$timestamp);
    }


    /**
     * 获取未来时间戳距离还有多少个自然天
     * @param int $timestamp
     * @return int
     */
    public static function getNatualDaysByTimestamp(int $timestamp): int
    {

        $days = ceil(($timestamp-time())/86400);

        return max(0,$days);
    }



    /**
     * 获取两个时间戳之间的自然月数
     * @param int $startTimestamp
     * @param int $endTimestamp
     * @return int
     */
    public static function getNaturalMonthNumber(int $startTimestamp, int $endTimestamp): int
    {
        list($startYear, $startMonth) = explode('-', date('Y-m', $startTimestamp));
        list($endYear, $endMonth) = explode('-', date('Y-m', $endTimestamp));

        if ($startYear == $endYear)
        {
            $monthNumber = $endMonth - $startMonth;
        }
        else
        {
            $yearInterval = $endYear - $startYear - 1;
            $monthNumber = (12 - $startMonth + $endMonth) + 12 * $yearInterval;
        }

        return $monthNumber + 1;
    }


    /**
     * 获取两个时间戳之间的自然天数
     * @param int $startTimestamp
     * @param int $endTimestamp
     * @return int
     */
    public static function getNatualDaysNumber(int $startTimestamp, int $endTimestamp): int
    {
        $dayTimestamp = 24 * 3600;
        $subTimestamp = $endTimestamp - $startTimestamp;

        $days = intval($subTimestamp / $dayTimestamp);
        $tmp = $startTimestamp + $days * $dayTimestamp;

        if (date('d', $tmp) != date('d', $endTimestamp))
        {
            return $days + 2;
        }

        return $days + 1;
    }
}
